﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace AI.Steering
{
    /// <summary>
    /// 聚集
    /// </summary>
    public class SteeringForCollect : Steering
    {
        private Radar radar;
        [Tooltip("聚集间距")]
        public float collectDistance = 1;
        private void Start()
        {
            radar = GetComponent<Radar>();
        }
        public override Vector3 GetForce()
        {
            //计算中心点
            Vector3 center = CalculateCenter();
            //计算期望力 = 目标点 - 自身点
            Vector3 expectForce = (center - transform.position).normalized * speed;
            
            //如果雷达扫描到目标 没有接近中心点
            if (radar.targetList.Count != 0 && Vector3.Distance(transform.position,center) > collectDistance)
            {
                //返回实际力 = 期望力 - 当前力
                return (expectForce - controller.currentForce) * weight;//权重越大，实际力到达最大越快
            }
            //返回0
            return Vector3.zero;
        }
        private Vector3 CalculateCenter()
        {
            Vector3 center = Vector3.zero;
            for (int i = 0; i < radar.targetList.Count; i++)
            {
                center += radar.targetList[i].position;
            }
            center /= radar.targetList.Count;
            return center;
        }
    }
}

